﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Behavior.Rotate
{
    public class RotationAdorner : Adorner
    {
        private readonly VisualCollection _visuals;
        private readonly RotateThumb _thumb;

        public RotationAdorner(UIElement adornedElement) : base(adornedElement)
        {
            _thumb = new RotateThumb();
            _thumb.DataContext = AdornedElement;
            FrameworkElementFactory ellipse = new FrameworkElementFactory(typeof(Ellipse));
            ellipse.SetValue(Ellipse.WidthProperty, 8d);
            ellipse.SetValue(Ellipse.HeightProperty, 8d);
            ellipse.SetValue(Ellipse.FillProperty, Brushes.Aquamarine);
            ellipse.SetValue(Ellipse.StrokeProperty, Brushes.Black);
            ellipse.SetValue(Ellipse.StrokeThicknessProperty, 1d);
            _thumb.Cursor = new Cursor(new System.IO.MemoryStream(Settings.cursorRotate));
            ControlTemplate controlTemplate = new ControlTemplate(typeof(RotateThumb));
            controlTemplate.VisualTree = ellipse;
            _thumb.Template = controlTemplate;
            _visuals = new VisualCollection(this);
            _visuals.Add(_thumb);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double adornerWidth = DesiredSize.Width;
            double adornerHeight = DesiredSize.Height;
            double adornedElWidth = AdornedElement.DesiredSize.Width;
            double adornedElHeight = AdornedElement.DesiredSize.Height;
            _thumb.Arrange(new Rect(0, -adornedElHeight/2 - 30, adornerWidth, adornerHeight));
            return finalSize;
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return _visuals.Count;
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            return _visuals[index];
        }
    }
}
